package com.hmall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.MallShippingMapper;
import com.hmall.model.MallShipping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ShippingService {

    @Autowired
    private MallShippingMapper shippingMapper;

    public ShippingService(MallShippingMapper shippingMapper) {
        this.shippingMapper = shippingMapper;
    }


    public ServerResponse add(Integer userId, MallShipping shipping) {

        if (shipping == null) {
            return ServerResponse.createByErrorMessage("创建地址失败");
        }
        shipping.setUserId(userId);

        shippingMapper.myInsert(shipping);
        return ServerResponse.createBySuccess("新建地址成功");
    }

    public ServerResponse update(Integer userId, MallShipping shipping){
        shipping.setUserId(userId);
        int rowCount = shippingMapper.updateByShipping(shipping);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("更新地址成功");
        }
        return ServerResponse.createByErrorMessage("更新地址失败");
    }

    public ServerResponse<MallShipping> select(Integer userId, Integer shippingId){
        MallShipping shipping = shippingMapper.selectByShippingIdUserId(userId,shippingId);
        if(shipping == null){
            return ServerResponse.createByErrorMessage("无法查询到该地址");
        }
        return ServerResponse.createBySuccess("更新地址成功",shipping);
    }


    public ServerResponse<PageInfo> list(Integer userId, int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<MallShipping> shippingList = shippingMapper.selectByUserId(userId);

        PageInfo pageInfo = new PageInfo(shippingList);
        return ServerResponse.createBySuccess(pageInfo);
    }


    public ServerResponse<List<MallShipping>> list(Integer userId) {
        List<MallShipping> shippingList = shippingMapper.selectByUserId(userId);

        return ServerResponse.createBySuccess(shippingList);
    }


}
